package com.rms.recruit_management_system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.rms.recruit_management_system.mapper.ResumeMapper;
import com.rms.recruit_management_system.pojo.Resume;
import com.rms.recruit_management_system.utils.Result;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.util.List;

@Slf4j
@RestController
public class PDFController {

    @Autowired
    private ResumeMapper resumeMapper;

    @RequestMapping("/resume/exportPdf/{userId}")
    public Result<?> exportResumesToPDF(@PathVariable Integer userId, HttpServletResponse response)   throws DocumentException {
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            // 创建 PDF 文档
            Document document = new Document();
            PdfWriter.getInstance(document, outputStream);
            document.open();

            // 设置字体，解决中文显示问题
            BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            Font font = new Font(baseFont);

            // 获取简历数据
            List<Resume> resumes = resumeMapper.selectList(new QueryWrapper<Resume>().eq("user_id", userId));
            for (Resume resume : resumes) {
                // 向 PDF 中添加内容
                document.add(new Paragraph("姓名: " + resume.getName(), font));
                document.add(new Paragraph("邮箱: " + resume.getEmail(), font));
                document.add(new Paragraph("电话: " + resume.getPhone(), font));
                document.add(new Paragraph("期望职位: " + resume.getIntention(), font));
                document.add(new Paragraph("个人简介: " + resume.getSummary(), font));
                document.add(new Paragraph("教育经历: " + resume.getEducation(), font));
                document.add(new Paragraph("工作经验: " + resume.getExperience(), font));
                document.add(new Paragraph("技能特长: " + resume.getSkills(), font));
                document.add(new Paragraph("\n"));
            }

            document.close();
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment;filename=resume.pdf");
            response.setContentLength(outputStream.size());
            outputStream.writeTo(response.getOutputStream());
            return new Result<>(200,"导出成功",outputStream.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(400,"导出失败");
        }
    }
}
